package cn.gson.springboot.controller.cuscontroller;

import cn.gson.springboot.aop.MyLog;
import cn.gson.springboot.model.pjos.Cuclue;
import cn.gson.springboot.model.service.cusservice.CuclueService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/vue")
public class CuclueController {
    @Autowired
    CuclueService cuclueService;//线索service

    //    分页查询线索
    @RequestMapping("/select-cuclue")
    @ResponseBody
    public Map<String,Object> pageSelectCuclue(Integer page, Integer size,Integer empId,Integer deptId, String cucStatus,String serch){
        System.out.println("cucStatus:"+cucStatus);
        System.out.println("serch:"+serch);
        System.out.println("线索查询参数："+empId+","+deptId);
        Page<Object> obj= PageHelper.startPage(page,size);
        cuclueService.pageSelectCuclue(empId,deptId,cucStatus,serch);

        Map<String,Object> map=new HashMap<>();
        map.put("rows",obj.getResult());//一页展示的数据
        map.put("totals",obj.getTotal());//总条数
        return map;
    }

    //    新增线索
    @MyLog(type = "线索",value = "新增了线索")
    @RequestMapping("/save-cuclue")
    @ResponseBody
    public void saveCuclue(String cuclue,Integer empId){
        JSONObject obj=(JSONObject)JSONObject.parse(cuclue);
        Cuclue cuclues=obj.getObject("cuclue",Cuclue.class);
        cuclueService.saveCuclue(cuclues,empId);
    }

    //    设为已失效线索
    @RequestMapping("/update-culue")
    @ResponseBody
    public void updateCulue(String cucId){
        System.out.println("cucId:"+cucId);
        JSONObject obj=(JSONObject)JSONObject.parse(cucId);
        Integer cucIds=obj.getObject("cucId",Integer.class);
        cuclueService.updateCulue(cucIds);
    }

    //    将线索新增为客户
    @RequestMapping("/add-customer")
    @ResponseBody
    public void addCustomer(String cuclue,String cusAddressProvince,String cusAddressCity,String cusAddressArea){
        System.err.println("hhh:"+cuclue);
        JSONObject obj=(JSONObject)JSONObject.parse(cuclue);
        Cuclue cuclue1=obj.getObject("cuclue",Cuclue.class);
        cuclueService.addCustomer(cuclue1,cusAddressProvince,cusAddressCity,cusAddressArea);
    }

    //    查询线索联系电话是否重复
    @RequestMapping("/select-cucupphone")
    @ResponseBody
    public Cuclue selectCucCupPhone(String cucLinkPhone){
        return cuclueService.selectCucCupPhone(cucLinkPhone);
    }
}
